import ash from '@ash.ts/ash';
import { _decorator, Component, resources, view } from 'cc';
import { MotionControlSystem } from './component/MotionControlSystem';
import { GameConfig } from './data/GameConfig';
import { DeathThroesSystem } from './system/DeathThroesSystem';
import { KeyPoll } from './input/KeyPoll';
import { AnimationSystem } from './system/AnimationSystem';
import { BulletAgeSystem } from './system/BulletAgeSystem';
import { CollisionSystem } from './system/CollisionSystem';
import { GameManager } from './system/GameManager';
import { GunControlSystem } from './system/GunControlSystem';
import { HudSystem } from './system/HudSystem';
import { MovementSystem } from './system/MovementSystem';
import { RenderSystem } from './system/RenderSystem';
import { SystemPriorities } from './system/SystemPriorities';
import { WaitForStartSystem } from './system/WaitForStartSystem';
import { EntityCreator } from './util/EntityCreator';
const { ccclass } = _decorator;


@ccclass('App')
export class App extends Component {

    start() {
        const { width, height } = view.getVisibleSize();
        const config = new GameConfig(width, height);
        const engine = new ash.Engine();
        const creator = new EntityCreator(engine);
        const keyPoll = new KeyPoll();
        // const audioContext = new AudioContext();
        // const audioDB = await loadAudioDB(audioContext);
        const tickProvider = new ash.FrameTickProvider();
        tickProvider.add((delta: number) => engine.update(delta));
        tickProvider.start();

        engine.addSystem(new WaitForStartSystem(creator), SystemPriorities.preUpdate);
        engine.addSystem(new GameManager(creator, config), SystemPriorities.preUpdate);
        engine.addSystem(new MotionControlSystem(keyPoll), SystemPriorities.update);
        engine.addSystem(new GunControlSystem(keyPoll, creator), SystemPriorities.update);
        engine.addSystem(new BulletAgeSystem(creator), SystemPriorities.update);
        engine.addSystem(new DeathThroesSystem(creator), SystemPriorities.update);
        engine.addSystem(new MovementSystem(config), SystemPriorities.move);
        engine.addSystem(new CollisionSystem(creator), SystemPriorities.resolveCollisions);
        engine.addSystem(new AnimationSystem(), SystemPriorities.animate);
        engine.addSystem(new HudSystem(), SystemPriorities.animate);
        engine.addSystem(new RenderSystem(this.node), SystemPriorities.render);

        resources.loadDir("prefab", (err, assets) => {
            if (!err) {
                creator.createWaitForClick();
                creator.createGame();
            }
        });
    }

    update(deltaTime: number) {

    }
}


